home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d1 / ansi_12.arc / ANSI.ASM next >
Assembly Source File  |  1989-06-29  |  67KB  |  1,353 lines

  1. ;------------------------------------------------------------------------;
  2. ;  ANSI.COM - Replacement for the ANSI.SYS console device driver.        ;
  3. ;  Unlike ANSI.SYS which must be installed at boot time, ANSI.COM        ;
  4. ;  can be installed and uninstalled at anytime.  Enhancements include    ;
  5. ;  a fast screen write and variable sized keyboard reassignment buffer.  ;
  6. ;                                                                        ;
  7. ;  Update 3/2/89 - Fix for DOS function 0Bh, Check Standard Input Status ;
  8. ;                      and STDIN in ANSI_INT_21 handler.                 ;
  9. ;                  Leading zero inserted for Device Status Report for    ;
  10. ;                      single digit cursor positions.                    ;
  11. ;                  INFORMATION typo 40 for 40H.                          ;
  12. ;                  WRITE_FAST modified to handle CR and LF instead of    ;
  13. ;                      WRITE_CHAR.                                       ;
  14. ;  Update 3/7/89 - Fix for CLS in graphics mode.                         ;
  15. ;                                                                        ;
  16. ;  PC Magazine - Michael J. Mefford                                      ;
  17. ;------------------------------------------------------------------------;
  18.  
  19. _TEXT          SEGMENT PUBLIC 'CODE'
  20.                ASSUME  CS:_TEXT,DS:_TEXT,ES:_TEXT,SS:_TEXT
  21.                ORG     100H
  22. START:         JMP     INITIALIZE
  23.  
  24. ;                 DATA AREA
  25. SIGNATURE      DB      CR,SPACE,SPACE,SPACE,CR,LF
  26. COPYRIGHT      DB      "ANSI 1.2 (C) 1988 Ziff Communications Co.",CR,LF
  27. PROGRAMMER     DB      "PC Magazine ",BOX," Michael J. Mefford",CR,LF,LF,"$"
  28.                DB      CTRL_Z
  29.  
  30. CR             EQU     13
  31. LF             EQU     10
  32. CTRL_Z         EQU     26
  33. SPACE          EQU     32
  34. BOX            EQU     254
  35. ESC_CHAR       EQU     27
  36. SINGLE_QUOTE   EQU     39
  37. DOUBLE_QUOTE   EQU     34
  38. BELL           EQU     7
  39. BS             EQU     8
  40. TAB            EQU     9
  41.  
  42. OFF            EQU     1
  43. ON             EQU     2
  44. SLOW           EQU     4
  45. FAST           EQU     8
  46. STATUS_MASK    EQU     11111100B
  47.  
  48. ANSI_STATE     DW      ESC_STATE
  49. STATUS         DB      ON OR FAST
  50. PARAMETERS     DB      "OFF ON  SLOWFAST"
  51. LAST_PARAMETER EQU     $ - PARAMETERS
  52.  
  53. OLD_INT_29     DW      ?,?
  54. OLD_INT_16     DW      ?,?
  55. OLD_INT_21     DW      ?,?
  56.  
  57. ATTRIBUTE      DB      7
  58. SAVE_POSITION  DW      0
  59. LINE_WRAP      DB      ON
  60. QUOTE_TYPE     DB      ?
  61. ESC_COUNT      DW      0
  62. NUMBER_COUNT   DW      0
  63.  
  64. COMMAND_TABLE  LABEL   BYTE
  65. DB   "H", "A", "B", "C", "D", "f", "n", "s", "u", "K", "m", "h", "l", "p", "J"
  66. COMMAND_LENGTH EQU     $ - COMMAND_TABLE
  67.  
  68. DW   CURS_POSITION, CURSOR_UP,     CURSOR_DOWN, CURS_FORWARD, CURS_BACKWARD
  69. DW   HORZ_VERT_POS, DEVICE_STATUS, SAVE_CURSOR, RESTORE_CURS, ERASE_IN_LINE
  70. DW   SGR,           SET_MODE,      RESET_MODE,  REASSIGNMENT, CLS
  71. COMMAND_END    EQU     $ - 2
  72.  
  73. ATTRIBUTE_TABLE        LABEL    BYTE
  74. DB   00,01,04,05,07,08,30,31,32,33,34,35,36,37,40,41,42,43,44,45,46,47
  75. ATTRIBUTE_LENGTH       EQU      $ - ATTRIBUTE_TABLE
  76.  
  77. ;Format: AND mask,OR mask
  78. DB      000H,07H, 0FFH,08H, 0F8H,01H, 0FFH,80H, 0F8H,70H, 088H,00H
  79. DB      0F8H,00H, 0F8H,04H, 0F8H,02H, 0F8H,06H, 0F8H,01H, 0F8H,05H
  80. DB      0F8H,03H, 0F8H,07H, 08FH,00H, 08FH,40H, 08FH,20H, 08FH,60H
  81. DB      08FH,10H, 08FH,50H, 08FH,30H, 08FH,70H
  82. ATTRIBUTE_END          EQU      $ - 2
  83.  
  84. BIOS_ACTIVE_PAGE       EQU     62H
  85. ACTIVE_PAGE    DB      ?
  86. ADDR_6845      DW      ?
  87. BIOS_CRT_MODE          EQU     49H
  88. CRT_MODE       DB      ?
  89. CRT_COLS       DW      ?
  90. CRT_LEN        DW      ?
  91. CRT_START      DW      ?
  92. CRT_DATA_LENGTH        EQU     $ - CRT_MODE
  93. CURSOR_POSN    LABEL   WORD
  94. CURSOR_COL     DB      ?
  95. CURSOR_ROW     DB      ?
  96. CRT_ROWS       DB      ?
  97.  
  98. DOS_INPUT      DB      OFF
  99. BUSY_FLAG      DB      OFF
  100. REASSIGN_FLAG  DB      OFF
  101. REMOVE_FLAG    DB      ON
  102. REASSIGN_COUNT DW      0
  103. REASSIGN_POS   DW      ?
  104. FUNCTION_16    DB      ?
  105. ZR             EQU     1000000B
  106.  
  107. ESC_BUFFER_SIZE      EQU     126
  108. REASSIGNMENT_DEFAULT EQU     200
  109. REASSIGNMENT_SIZE    DW      REASSIGNMENT_DEFAULT
  110. REASSIGNMENT_MAX     EQU     60 * 1024
  111. REASSIGN_END         DW      REASSIGNMENT_BUFFER
  112. BUFFER_END           DW      REASSIGNMENT_BUFFER + REASSIGNMENT_DEFAULT
  113.  
  114. ;                   CODE AREA
  115. ;************* INTERRUPT HANDLERS *************;
  116. ;------------------------------------------------------------------------------;
  117. ; INT 29 is an undocumented interrupt called by DOS for output to the console. ;
  118. ;------------------------------------------------------------------------------;
  119.  
  120. ANSI_INT_29    PROC    FAR
  121.                PUSH    AX                      ;Save all registers.
  122.                PUSH    BX
  123.                PUSH    CX
  124.                PUSH    DX
  125.                PUSH    SI
  126.                PUSH    DI
  127.                PUSH    DS
  128.                PUSH    ES
  129.                PUSH    BP
  130.  
  131.                CLD                             ;All string operations forward.
  132.                MOV     BX,CS                   ;Point to our data segment.
  133.                MOV     DS,BX
  134.                MOV     ES,BX
  135.                CALL    ANSI_STATE              ;Call the current state of
  136.                                                ; the ANSI Esc sequence.
  137.                POP     BP
  138.                POP     ES
  139.                POP     DS
  140.                POP     DI
  141.                POP     SI
  142.                POP     DX
  143.                POP     CX
  144.                POP     BX
  145.                POP     AX                      ;Restore all registers and
  146.                IRET                            ; return.
  147. ANSI_INT_29    ENDP
  148.  
  149. ;------------------------------------------------;
  150.  
  151. ANSI_INT_21    PROC    FAR
  152.                PUSHF
  153.                CMP     AH,0BH                  ;If DOS function 0Bh (Check
  154.                JNZ     CK_INPUT                ; Standard Input Status)
  155.                CMP     CS:REASSIGN_FLAG,ON     ; and reassignment in progress,
  156.                JNZ     CK_INPUT                ; including a Device Status
  157.                CALL    DWORD PTR CS:OLD_INT_21 ; Request, then process call in
  158.                MOV     AL,0FFH                 ; case control break pressed;
  159.                IRET                            ; then return 0FFh for char ready
  160.  
  161. CK_INPUT:      CMP     AH,3FH                  ;Read from STDIN?
  162.                JNZ     CK_CONSOLE
  163.                OR      BX,BX
  164.                JZ      INPUT
  165.  
  166. CK_CONSOLE:    CMP     AH,0AH                  ;If DOS Int 21 functions Ah,
  167.                JZ      INPUT                   ; 7h, 1h or console input (6h)
  168.                CMP     AH,7                    ; then tell INT 16, DOS input
  169.                JZ      INPUT                   ; is active.
  170.                CMP     AH,1
  171.                JZ      INPUT
  172.                CMP     AH,8
  173.                JZ      INPUT
  174.                CMP     AH,6
  175.                JNZ     QUICK21_EXIT
  176.                CMP     DL,0FFH
  177.                JZ      INPUT
  178. QUICK21_EXIT:  POPF                              ;If not, let the original
  179.                JMP     DWORD PTR CS:OLD_INT_21   ; interrupt process the call.
  180.  
  181. INPUT:         POPF
  182.                MOV     CS:DOS_INPUT,ON           ;INT 16 handler flag.
  183.                PUSHF
  184.                CALL    DWORD PTR CS:OLD_INT_21   ;Emulate an interrupt.
  185.                MOV     CS:DOS_INPUT,OFF          ;Turn flag back off.
  186.                RET     2                         ;Return with current flags.
  187. ANSI_INT_21    ENDP
  188.  
  189. ;-----------------------------------------------------------------;
  190. ; If we get here via a DOS console input call, any awaiting key   ;
  191. ; in keyboard buffer is checked to see if it is to be reassigned. :
  192. ;-----------------------------------------------------------------;
  193.  
  194. ANSI_INT_16    PROC    FAR
  195.                STI                             ;Interrupts back on.
  196.                PUSHF                           ;Preserve flags on stack.
  197.                PUSH    BP
  198.                PUSH    DS
  199.                PUSH    AX
  200.                PUSH    CS                      ;Point to our data.
  201.                POP     DS
  202.  
  203.                CMP     DOS_INPUT,OFF           ;If not called via a DOS
  204.                JZ      QUICK16_EXIT            ; console input call, exit.
  205.                CMP     BUSY_FLAG,ON            ;If already processing a call,
  206.                JZ      QUICK16_EXIT            ; exit.
  207.                CLD                             ;All string operations forward.
  208.                MOV     BP,SP                   ;Base reference to flags on stack
  209.                MOV     FUNCTION_16,AH          ;Save function request.
  210.                AND     AH,NOT 10H              ;Strip possible extended request.
  211.                JZ      GET_KEY                 ;If zero, it's wait for a key.
  212.                DEC     AH                      ;Else, decrement.  If zero it's
  213.                JZ      KEY_STATUS              ; key status, else exit.
  214.  
  215. QUICK16_EXIT:  POP     AX                      ;Restore registers and let
  216.                POP     DS                      ; original interrupt handler
  217.                POP     BP                      ; process the call.
  218.                POPF
  219.                JMP     DWORD PTR CS:OLD_INT_16
  220.  
  221. KEY_STATUS:    OR      BYTE PTR [BP+6],ZR      ;Assume none ready; ZR = 1.
  222. GET_KEY:       MOV     BUSY_FLAG,ON            ;Non-reentrant; flag busy.
  223.                POP     AX
  224.                PUSH    BX                      ;Save some more registers.
  225.                PUSH    CX
  226.                PUSH    DX
  227.                PUSH    SI
  228.                PUSH    DI
  229.                CMP     REASSIGN_FLAG,ON        ;Already in process of reassign?
  230.                JZ      CK_BUFFER               ;If yes, check kbd buffer.
  231.  
  232.                PUSHF                           ;Else, emulate an interrupt.
  233.                CALL    DWORD PTR OLD_INT_16
  234.                PUSHF                           ;Status call results in flags.
  235.                TEST    FUNCTION_16,1           ;Was it a status call?
  236.                JZ      CK_DUP                  ;If no, check key returned.
  237.                POPF                            ;Else, retrieve status results.
  238.                JZ      INT16_EXIT              ;If zero, no key waiting.
  239.                AND     BYTE PTR [BP+6],NOT ZR  ;Else, ZR = 0.
  240.                PUSHF                           ;PUSHF just to keep stack right.
  241.  
  242. CK_DUP:        POPF                            ;Fix stack.
  243.                MOV     BX,AX                   ;Match procedure wants key in BX.
  244.                CALL    CK_MATCH                ;Check reassignment buffer.
  245.                JC      INT16_EXIT              ;If no match, exit.
  246.                MOV     AX,[DI]                 ;Else, retrieve string length.
  247.                SUB     AX,3                    ;Adjust for length word and match
  248.                ADD     DI,3                    ; byte; same for string pointer.
  249.                OR      BL,BL                   ;Extended key? ie. key = 0.
  250.                JNZ     STORE_COUNT             ;If no, save length and pointer.
  251.                DEC     AX                      ;Else, adjust for extended code.
  252.                INC     DI
  253. STORE_COUNT:   MOV     REASSIGN_COUNT,AX       ;Save string length.
  254.                MOV     REASSIGN_POS,DI         ;Save pointer to string.
  255.                MOV     REASSIGN_FLAG,ON        ;Flag that replacement in effect.
  256. CK_BUFFER:     TEST    FUNCTION_16,1           ;Was it a key wait function call?
  257.                JNZ     RETRIEVE                ;If no, key status; get it.
  258.                CMP     REMOVE_FLAG,OFF         ;Removed it from kbd buffer?
  259.                JZ      RETRIEVE                ;If yes, get reassignment.
  260.                MOV     AH,FUNCTION_16          ;Else, eat the replaced character
  261.                INT     16H                     ; via INT 16.
  262.                MOV     REMOVE_FLAG,OFF         ;Flag character eaten.
  263.  
  264. RETRIEVE:      MOV     DI,REASSIGN_POS         ;Retrieve pointer to string.
  265.                MOV     AX,[DI]                 ;Retrieve replacement character.
  266.                TEST    FUNCTION_16,1           ;Is it a key wait function call?
  267.                JZ      REMOVE                  ;If yes, bump pointer up one.
  268.                AND     BYTE PTR [BP+6],NOT ZR  ;If no, status call; ZR = 0
  269.                JMP     SHORT INT16_EXIT        ;All done.
  270.  
  271. REMOVE:        INC     REASSIGN_POS            ;Move pointer to next character.
  272.                DEC     REASSIGN_COUNT          ;One less character to replace.
  273.                JZ      REASSIGN_DONE           ;If end of string, reset flags.
  274.                OR      AL,AL                   ;Else, extended replacement?
  275.                JNZ     INT16_EXIT              ;If no, done here.
  276.                INC     REASSIGN_POS            ;Else adjust pointers.
  277.                DEC     REASSIGN_COUNT
  278.                JNZ     INT16_EXIT              ;End of string?
  279. REASSIGN_DONE: MOV     REASSIGN_FLAG,OFF       ;If yes, reset flags.
  280.                MOV     REMOVE_FLAG,ON
  281.  
  282. INT16_EXIT:    MOV     BUSY_FLAG,OFF           ;Reset the busy flag.
  283.                POP     DI                      ;Restore registers.
  284.                POP     SI
  285.                POP     DX
  286.                POP     CX
  287.                POP     BX
  288.                POP     DS
  289.                POP     BP
  290.                POPF                            ;Flags on stack have status
  291.                RET     2                       ; results; kill old flags.
  292. ANSI_INT_16    ENDP
  293.  
  294. ;************* ANSI ESCAPE STATE ROUTINES ************* ;
  295.  
  296. ESC_STATE:     MOV     BX,OFFSET BRACKET_STATE ;Assume Esc character.
  297.                CMP     AL,ESC_CHAR             ;Is it Esc?  If yes, store
  298.                JZ      SHORT_JUMP              ; char. and next state.
  299.                JMP     WRITE_CHAR              ;Else, normal char; write it.
  300.  
  301. ;------------------------------------------------;
  302.  
  303. BRACKET_STATE: MOV     BX,OFFSET SPECIAL_STATE ;Assume left bracket.
  304.                CMP     AL,"["                  ;Is it left bracket?  If yes,
  305. SHORT_JUMP:    JZ      STORE_STATE             ;store char. and next state.
  306.                JMP     FLUSH_BUFFER            ;Else, flush Esc out of buffer.
  307.  
  308. ;---------------------------------------------------------------;
  309. ; Must process <ESC>[2J (CLS) regardless if ANSI.COM ON or OFF. ;
  310. ;---------------------------------------------------------------;
  311.  
  312. SPECIAL_STATE: MOV     BX,OFFSET CLS_STATE     ;Assume Erase in Display code.
  313.                CMP     AL,"2"                  ;Is it the "2" ?
  314.                JZ      STORE_STATE             ;If yes, progress to next state.
  315.                TEST    STATUS,OFF              ;Else, is ANSI OFF ?
  316.                JNZ     FLUSH_BUFFER            ;If yes, flush Esc, bracket.
  317.                MOV     BX,OFFSET PARAM_STATE   ;Else, check for first Set,
  318.                CMP     AL,"="                  ; Reset Mode characters of
  319.                JZ      STORE_STATE             ; "=" and "?".
  320.                CMP     AL,"?"
  321.                JZ      STORE_STATE             ;If found, store.
  322.                JMP     SHORT DO_PARAMETER      ;Else, check other codes.
  323.  
  324. ;------------------------------------------------;
  325.  
  326. CLS_STATE:     CMP     AL,"J"                  ;Is it second character of CLS?
  327.                MOV     DI,OFFSET COMMAND_END   ;If yes, execute
  328.                JZ      EXECUTE                 ; Erase in Display procedure.
  329.                TEST    STATUS,OFF              ;ANSI OFF?  If yes, flush buffer.
  330.                JNZ     FLUSH_BUFFER            ; If not fall through to process.
  331.                MOV     BYTE PTR NUMBER_BUFFER,2   ;Store the previous 2.
  332. DO_PARAMETER:  MOV     ANSI_STATE,OFFSET PARAM_STATE  ;Parameter state.
  333.  
  334. ;------------------------------------------------;
  335.  
  336. PARAM_STATE:   CALL    CK_QUOTE                ;Is it single or double quotes?
  337.                JZ      STORE_STATE             ;If yes, store string state.
  338.                CMP     AL,";"                  ;Is it semi-colon delimiter?
  339.                JNZ     CK_NUMBER               ;If no, check if number.
  340.                INC     NUMBER_COUNT            ;Else, increment number count.
  341.                JMP     SHORT BUFFER_CHAR       ;Buffer the semi-colon.
  342.  
  343. CK_NUMBER:     CMP     AL,"0"                  ;Is it below 0 ?
  344.                JB      FLUSH_BUFFER            ;If yes, illegal; flush buffer.
  345.                CMP     AL,"9"                  ;Else, is it above 9 ?
  346.                JA      DO_COMMAND              ;If yes, check for command char.
  347.                CALL    ACCUMULATE              ;Else it's a number; accumulate.
  348.                JMP     SHORT BUFFER_CHAR       ;Buffer the character.
  349.  
  350. DO_COMMAND:    MOV     DI,OFFSET COMMAND_TABLE ;Point to legal ANSI commands.
  351.                MOV     CX,COMMAND_LENGTH       ;Number of commands.
  352.                REPNZ   SCASB                   ;Check for a match.
  353.                JNZ     FLUSH_BUFFER            ;If no match, flush sequence.
  354.                INC     NUMBER_COUNT            ;Else, increment for last number.
  355.                MOV     DI,OFFSET COMMAND_END   ;Point to appropriate command
  356.                SHL     CX,1                    ; processing procedure.
  357.                SUB     DI,CX
  358. EXECUTE:       CALL    GET_BIOS_DATA           ;Get cursor position data.
  359.                CALL    DS:[DI]                 ;Do command subroutine.
  360.                JMP     SHORT FLUSH_END         ;Clear buffer.
  361.  
  362. ;------------------------------------------------;
  363.  
  364. QUOTE_STATE:   CMP     AL,QUOTE_TYPE           ;Is it an ending string quote?
  365.                JNZ     BUFFER_CHAR             ;If no, buffer literal.
  366.                MOV     BX,OFFSET PARAM_STATE   ;Else, back to parameter parsing.
  367.  
  368. ;------------------------------------------------;
  369.  
  370. STORE_STATE:   MOV     ANSI_STATE,BX           ;Store the ANSI state.
  371.  
  372. ;------------------------------------------------;
  373.  
  374. BUFFER_CHAR:   MOV     DI,ESC_COUNT            ;Buffer the character in case
  375.                CMP     DI,ESC_BUFFER_SIZE      ; ending ANSI command is illegal.
  376.                JZ      FLUSH_BUFFER            ;If buffer full, flush.
  377.                ADD     DI,OFFSET ESC_BUFFER    ;Point to next buffer position.
  378.                STOSB                           ;Store the character.
  379.                INC     ESC_COUNT               ;Increment the sequence count.
  380.                RET
  381.  
  382. ;------------------------------------------------;
  383.  
  384. FLUSH_BUFFER:  PUSH    AX                      ;Save the current character.
  385.                MOV     SI,OFFSET ESC_BUFFER    ;Point to the sequence buffer.
  386.                MOV     CX,ESC_COUNT            ;Count of buffered characters.
  387. NEXT_FLUSH:    LODSB                           ;Retrieve one.
  388.                PUSH    CX                      ;Save counter and pointer.
  389.                PUSH    SI
  390.                CALL    WRITE_CHAR              ;Write character to screen.
  391.                POP     SI                      ;Restore counter and pointer.
  392.                POP     CX
  393.                LOOP    NEXT_FLUSH              ;Flush entire buffer.
  394.                POP     AX                      ;Retrieve last character.
  395.                CALL    WRITE_CHAR              ;Write it also.
  396. FLUSH_END:     MOV     ESC_COUNT,0                   ;Reset counter.
  397.                MOV     ANSI_STATE,OFFSET ESC_STATE   ;Back to Esc state.
  398.                MOV     NUMBER_COUNT,0                ;Reset parameter counter.
  399.                PUSH    CS                            ;Point to our data.
  400.                POP     ES
  401.                MOV     DI,OFFSET NUMBER_BUFFER       ;Clear number buffer
  402.                MOV     CX,ESC_BUFFER_SIZE / 2        ; to zeros.
  403.                XOR     AX,AX
  404.                REP     STOSW
  405.                RET
  406.  
  407. ;------------------------------------------------;
  408. ; Slow video writes are via BIOS Write TTY.      ;
  409. ;------------------------------------------------;
  410.  
  411. WRITE_CHAR:    CMP     AL,BELL                 ;Let BIOS process BS and BELL.
  412.                JZ      WRITE_TTY
  413.                CMP     AL,BS
  414.                JZ      WRITE_TTY
  415.  
  416.                CALL    GET_BIOS_DATA           ;Get BIOS video data.
  417.                CMP     AL,TAB                  ;Is character a TAB?
  418.                JNZ     CK_ACTIVE               ;If no, process normally.
  419.                MOV     CX,CURSOR_POSN          ;Else, expand TAB to
  420.                AND     CX,7                    ; appropriate space characters.
  421.                NEG     CX
  422.                ADD     CX,8
  423. NEXT_TAB:      PUSH    CX
  424.                MOV     AL,SPACE
  425.                CALL    CK_ACTIVE
  426.                POP     CX
  427.                LOOP    NEXT_TAB
  428.                RET
  429.  
  430. CK_ACTIVE:     TEST    STATUS,OFF              ;Is ANSI OFF?
  431.                JNZ     WRITE_TTY               ;If yes, write via BIOS TTY.
  432. CK_FAST:       CALL    CK_SLOW_TEXT            ;Is ANSI SLOW or in graphics
  433.                JNC     WRITE_FAST              ; mode? If no, write fast.
  434.  
  435.                CMP     AL,CR                   ;Let BIOS handle CR and LF.
  436.                JZ      WRITE_TTY
  437.                CMP     AL,LF
  438.                JZ      WRITE_TTY
  439.  
  440. WRITE_SLOW:    PUSH    AX                      ;Else, write character/attribute
  441.                MOV     CX,1                    ; at current cursor position
  442.                MOV     BH,ACTIVE_PAGE          ; via BIOS.
  443.                MOV     BL,ATTRIBUTE
  444.                MOV     AH,9
  445.                INT     10H
  446.                POP     AX
  447.  
  448.                CMP     LINE_WRAP,ON            ;Is line wrap on?
  449.                JZ      TTY                     ;If yes, continue.
  450.                MOV     CX,CRT_COLS             ;Else, cursor at rightmost
  451.                DEC     CL                      ; column?
  452.                CMP     CL,CURSOR_COL           ;If yes, continue, else
  453.                JNZ     TTY                     ; return without writing.
  454.                RET
  455.  
  456. ;------------------------------------------------;
  457.  
  458. WRITE_TTY:     MOV     BL,7                    ;Attribute in graphics mode.
  459. TTY:           MOV     AH,0EH
  460.                INT     10H
  461.                RET
  462.  
  463. ;----------------------------------------------------------------------------;
  464. ; Fast screen writes are directly to the video buffer without retrace check. ;
  465. ;----------------------------------------------------------------------------;
  466.  
  467. WRITE_FAST:    PUSH    ES                      ;Preserve extra segment.
  468.                MOV     DX,CURSOR_POSN          ;Retrieve cursor position.
  469.                CMP     AL,CR                   ;Carriage return?
  470.                JNZ     CK_LINEFEED             ;If no, check linefeed.
  471.                XOR     DL,DL                   ;Else, cursor to first column.
  472.                JMP     SHORT UPDATE_CURSOR
  473. CK_LINEFEED:   CALL    VIDEO_SETUP             ;Calculate video address.
  474.                CMP     AL,LF                   ;Linefeed?
  475.                JZ      NEXT_ROW                ;If yes, next row.
  476.  
  477.                MOV     AH,ATTRIBUTE            ;Retrieve attribute.
  478.                STOSW                           ;Put char/attrib in video buffer.
  479.                INC     DL                      ;Increment cursor column.
  480.                CMP     DL,BYTE PTR CRT_COLS    ;End of row?
  481.                JB      UPDATE_CURSOR           ;If no, update cursor.
  482.  
  483.                CMP     LINE_WRAP,OFF           ;Else, line wrap off?
  484.                JZ      FAST_END                ;If yes, don't move cursor.
  485.                XOR     DL,DL                   ;Else, column zero.
  486. NEXT_ROW:      INC     DH                      ;Next row.
  487.                CALL    INFORMATION             ;Get displayable row info.
  488.                CMP     DH,AL                   ;Beyond the bottom of screen?
  489.                JBE     UPDATE_CURSOR           ;If no, update cursor.
  490.  
  491.                DEC     DH                      ;Else, cursor to original row.
  492.                MOV     DI,CRT_START            ;Point destination to top.
  493.                MOV     SI,DI                   ;Point source to second row
  494.                MOV     AX,CRT_COLS             ; by adding width in columns
  495.                PUSH    AX                      ; twice for char/attribute.
  496.                ADD     SI,AX
  497.                ADD     SI,AX
  498.                MUL     DH                      ;Times displayable rows - 1.
  499.                MOV     CX,AX                   ; equals char/attrib to scroll.
  500.                PUSH    DS                      ;Save data segment and
  501.                PUSH    ES                      ; point to video segment.
  502.                POP     DS
  503.                REP     MOVSW                   ;Scroll the screen.
  504.                POP     DS                      ;Restore data segment.
  505.                POP     CX                      ;Retrieve CRT columns.
  506.                MOV     AL,SPACE                ;Write space/attrib to
  507.                MOV     AH,ATTRIBUTE            ; bottom row.
  508.                REP     STOSW
  509.  
  510. UPDATE_CURSOR: CALL    SET_CURSOR              ;Update the cursor position.
  511. FAST_END:      POP     ES                      ;Restore extra segment.
  512.                RET
  513.  
  514. ;************* SUPPORT ROUTINES *************;
  515.  
  516. CK_QUOTE:      MOV     BX,OFFSET QUOTE_STATE   ;Assume quote state.
  517.                MOV     AH,DOUBLE_QUOTE
  518.                CMP     AL,AH                   ;Is it double quote?
  519.                JZ      GOT_QUOTE               ;If yes, string delimiter.
  520.                MOV     AH,SINGLE_QUOTE         ;Is it single quote?
  521.                CMP     AL,AH                   ;Is yes, string delimiter.
  522.                JNZ     QUOTE_END               ;Else, return ZR = 0.
  523. GOT_QUOTE:     MOV     QUOTE_TYPE,AH           ;Store as matching string end.
  524. QUOTE_END:     RET
  525.  
  526. ;------------------------------------------------;
  527.  
  528. ACCUMULATE:    PUSH    AX                      ;Preserve number character.
  529.                SUB     AL,"0"                  ;Convert ASCII to binary.
  530.                MOV     CL,AL                   ;Save the number.
  531.                MOV     AX,10                   ;Multiply previous count by 10.
  532.                MOV     BX,NUMBER_COUNT
  533.                MUL     BYTE PTR NUMBER_BUFFER[BX]
  534.                ADD     AL,CL                            ;Add in new number
  535.                MOV     BYTE PTR NUMBER_BUFFER[BX],AL    ; and store.
  536.                POP     AX
  537.                RET
  538.  
  539. ;---------------------------------------------------------------------------;
  540. ; OUTPUT:  CY = 1 if write SLOW mode or in graphics mode; CY = 0 otherwise. ;
  541. ;---------------------------------------------------------------------------;
  542.  
  543. CK_SLOW_TEXT:  TEST    STATUS,SLOW
  544.                JNZ     SLOW_MODE
  545.                CMP     CRT_MODE,7
  546.                JZ      TEXT_MODE
  547.                CMP     CRT_MODE,3
  548.                JA      SLOW_MODE
  549. TEXT_MODE:     CLC
  550.                RET
  551.  
  552. SLOW_MODE:     STC
  553.                RET
  554.  
  555. ;-------------------------------------;
  556. ; OUTPUT:  AL = Screen rows minus one ;
  557. ;-------------------------------------;
  558.  
  559. INFORMATION:   PUSH    DS                      ;Save data segment.
  560.                MOV     AX,40H                  ;Point to BIOS data.
  561.                MOV     DS,AX
  562.                MOV     AL,DS:[84H]             ;Retrieve rows - 1.
  563.                OR      AL,AL                   ;BIOS supported?
  564.                JNZ     INFO_END                ;If yes, done here.
  565.                MOV     AL,24                   ;Else, assume 25 lines.
  566. INFO_END:      POP     DS
  567.                RET
  568.  
  569. ;------------------------------------------------------------------------------;
  570. ; INPUT:  DX = Cursor position.                                                ;
  571. ; OUTPUT: ES = Video buffer segment; DI = Video buffer offset; AX,DX preserved ;
  572. ;------------------------------------------------------------------------------;
  573.  
  574. VIDEO_SETUP:   PUSH    AX
  575.                MOV     AX,CRT_COLS             ;Retrieve CRT columns.
  576.                MUL     DH                      ;Times cursor row.
  577.                MOV     BL,DL
  578.                XOR     BH,BH
  579.                ADD     AX,BX                   ;Plus cursor column.
  580.                SHL     AX,1                    ;Times two for attribute.
  581.                MOV     DI,CRT_START            ;Plus starting video offset.
  582.                ADD     DI,AX                   ;Equals destination.
  583.  
  584.                MOV     BX,0B000H               ;Assume mono card.
  585.                CMP     ADDR_6845,3B4H          ;Is it mono port?
  586.                JZ      VIDEO_SEGMENT           ;If yes, guessed right.
  587.                ADD     BX,800H                 ;Else, point to color segment.
  588. VIDEO_SEGMENT: MOV     ES,BX
  589.                POP     AX
  590.                RET
  591.  
  592. ;------------------------------------------------;
  593. ; Move BIOS video data into our data segment.    ;
  594. ;------------------------------------------------;
  595.  
  596. GET_BIOS_DATA: PUSH    DS
  597.                PUSH    DI                      ;Point to BIOS data segment.
  598.                MOV     BX,40H
  599.                MOV     DS,BX
  600.                MOV     SI,BIOS_ACTIVE_PAGE     ;Start with active page.
  601.                MOV     DI,OFFSET ACTIVE_PAGE
  602.                MOVSB                           ;Retrieve active page
  603.                MOVSW                           ; and address of 6845 port.
  604.                MOV     SI,BIOS_CRT_MODE        ;Retrieve CRT mode, CRT columns,
  605.                MOV     CX,CRT_DATA_LENGTH      ; CRT length, CRT start.
  606.                REP     MOVSB
  607.                MOV     BL,ES:ACTIVE_PAGE       ;Use active page as index
  608.                XOR     BH,BH                   ; of active cursor position.
  609.                SHL     BX,1
  610.                ADD     SI,BX
  611.                MOVSW
  612.                POP     DI
  613.                POP     DS
  614.                RET
  615.  
  616. ;----------------------------------------------------------------------------;
  617. ; OUTPUT: BL = First parameter; BH = Second parameter; DX = cursor position. ;
  618. ;----------------------------------------------------------------------------;
  619.  
  620. ADJUST_NUMBER: MOV     BX,WORD PTR NUMBER_BUFFER
  621.                OR      BH,BH
  622.                JNZ     ADJUST_AL               ;If either parameter zero,
  623.                INC     BH                      ; increment to convert
  624. ADJUST_AL:     OR      BL,BL                   ; to base one.
  625.                JNZ     ADJUST_END
  626.                INC     BL
  627. ADJUST_END:    MOV     DX,CURSOR_POSN          ;Return cursor position.
  628.                RET
  629.  
  630. ;--------------------------------------------------------------------------;
  631. ; INPUT:  AX = number; BP = 0 if console output; BP = 1 if storage output. ;
  632. ;--------------------------------------------------------------------------;
  633.  
  634. DECIMAL_OUT:   MOV     BX,10                   ;Divisor of ten.
  635.                XOR     CX,CX                   ;Zero in counter.
  636. NEXT_COUNT:    XOR     DX,DX                   ;Zero in high half.
  637.                DIV     BX                      ;Divide by ten.
  638.                ADD     DL,"0"                  ;Convert to ASCII.
  639.                PUSH    DX                      ;Save results.
  640.                INC     CX                      ;Also increment count.
  641.                CMP     AX,0                    ;Are we done?
  642.                JNZ     NEXT_COUNT              ;Continue until zero.
  643.                OR      BP,BP                   ;If BP zero, output to screen.
  644.                JNZ     DEVICE_OUTPUT           ;Else, store in buffer.
  645.  
  646. REPORT_OUTPUT: POP     AX                      ;Retrieve number.
  647.                CALL    PRINT_CHAR              ;Display it.
  648.                LOOP    REPORT_OUTPUT
  649.                RET
  650.  
  651. DEVICE_OUTPUT: CMP     CX,2                    ;Two digits?
  652.                JZ      TWO_DIGITS              ;If yes, process normally.
  653.                MOV     AL,"0"                  ;Else, store leading zero.
  654.                STOSB
  655.  
  656. TWO_DIGITS:    POP     AX                      ;Retrieve number.
  657.                STOSB                           ;Store it
  658.                LOOP    TWO_DIGITS
  659.                RET
  660.  
  661. ;************* ANSI COMMAND SUBSET *************;
  662.  
  663. CLS:           MOV     BH,7                    ;Assume normal attribute.
  664.                MOV     BL,BYTE PTR CRT_MODE    ;Get current video mode.
  665.                CMP     BL,4
  666.                JBE     CK_CLS_STATUS           ;If text mode then check if
  667.                CMP     BL,7                    ; ANSI active.
  668.                JZ      CK_CLS_STATUS
  669.                XOR     BH,BH                   ;Else, use black attribute
  670.                TEST    STATUS,OFF
  671.                JNZ     CLS_SLOW                ;If ANSI inactive, via BIOS.
  672.                JMP     SHORT CK_CLS_SLOW       ;Else, check if FAST or SLOW.
  673.  
  674. CK_CLS_STATUS: TEST    STATUS,OFF              ;Is ANSI OFF?
  675.                JNZ     CLS_SLOW                ;If yes, CLS via BIOS.
  676.                MOV     BH,ATTRIBUTE            ;Else, current attribute.
  677. CK_CLS_SLOW:   CALL    CK_SLOW_TEXT
  678.                JC      CLS_SLOW                ;If ANSI SLOW, via BIOS.
  679.  
  680. CLS_FAST:      MOV     AH,BH                   ;Else, attribute in high half.
  681.                MOV     AL,SPACE                ;Space character in low half.
  682.                XOR     DX,DX                   ;Cursor position home.
  683.                CALL    VIDEO_SETUP             ;Calculate video address.
  684.                MOV     CX,CRT_LEN              ;Retrieve CRT length.
  685.                SHR     CX,1                    ;Times two for attribute.
  686.                REP     STOSW                   ;Write directly to video buffer.
  687.                JMP     SHORT SET_CURSOR        ;Set the cursor top left corner.
  688.  
  689. CLS_SLOW:      CALL    INFORMATION             ;Get displayable rows.
  690.                MOV     DH,AL                   ;Store in DH.
  691.                MOV     DL,BYTE PTR CRT_COLS    ;Retrieve CRT columns.
  692.                DEC     DL                      ;Adjust to zero base.
  693.                XOR     CX,CX                   ;Scroll active page.
  694.                MOV     AX,600H
  695.                INT     10H
  696.                XOR     DX,DX                   ;Home the cursor.
  697.                JMP     SHORT SET_CURSOR
  698.  
  699. ;------------------------------------------------;
  700. CURS_POSITION:                                 ;These two commands are the same.
  701. HORZ_VERT_POS: CALL    INFORMATION             ;Returns AL = screen rows.
  702.                CALL    ADJUST_NUMBER           ;Returns BL,BH = parameters.
  703.                SUB     BX,101H                 ;Zero based.
  704.                CMP     BL,AL                   ;If cursor request within
  705.                JA      CURSOR_END              ; boundaries of screen, set it.
  706.                CMP     BH,BYTE PTR CRT_COLS
  707.                JAE     CURSOR_END
  708.                XCHG    BH,BL
  709.                MOV     DX,BX
  710.  
  711. SET_CURSOR:    MOV     BH,ACTIVE_PAGE          ;Set cursor via BIOS.
  712.                MOV     AH,2
  713.                INT     10H
  714. CURSOR_END:    RET
  715.  
  716. ;------------------------------------------------;
  717.  
  718. CURSOR_UP:     CALL    ADJUST_NUMBER           ;Move cursor up one or more rows
  719.                OR      DH,DH                   ; without changing column.
  720.                JZ      CURSOR_END              ;If already at top, ignore.
  721.                SUB     DH,BL
  722.                JNC     SET_CURSOR              ;Stay in bounds.
  723.                XOR     DH,DH
  724.                JMP     SHORT SET_CURSOR
  725.  
  726. ;------------------------------------------------;
  727.  
  728. CURSOR_DOWN:   CALL    INFORMATION             ;Returns AL = screen rows.
  729.                CALL    ADJUST_NUMBER           ;Returns BL,BH =nos.; DX = cursor
  730.                CMP     DH,AL                   ;Move cursor down requested
  731.                JZ      CURSOR_END              ; rows without changing column.
  732.                ADD     DH,BL
  733.                CMP     DH,AL
  734.                JNA     SET_CURSOR
  735.                MOV     DH,AL                   ;Stay in bounds.
  736.                JMP     SHORT SET_CURSOR
  737.  
  738. ;------------------------------------------------;
  739.  
  740. CURS_FORWARD:  CALL    ADJUST_NUMBER           ;Returns BL,BH =nos.; DX = cursor
  741.                MOV     BH,BYTE PTR CRT_COLS    ;Retrieve displayable columns.
  742.                DEC     BH                      ;Adjust to zero base.
  743.                CMP     DL,BH                   ;Move cursor forward one or more
  744.                JZ      CURSOR_END              ; columns without changing row.
  745.                ADD     DL,BL
  746.                CMP     DL,BH
  747.                JNA     SET_CURSOR
  748.                MOV     DL,BH                   ;Stay in bounds.
  749.                JMP     SHORT SET_CURSOR
  750.  
  751. ;------------------------------------------------;
  752.  
  753. CURS_BACKWARD: CALL    ADJUST_NUMBER           ;Returns BL,BH =nos.; DX = cursor
  754.                OR      DL,DL                   ;Move cursor backward one or
  755.                JZ      CURSOR_END              ; more columns without changing
  756.                SUB     DL,BL                   ; row.  Ignore if already left.
  757.                JNC     SET_CURSOR
  758.                XOR     DL,DL                   ;Stay in bounds.
  759.                JMP     SHORT SET_CURSOR
  760.  
  761. ;--------------------------------------------------------;
  762. ; Report cursor position via console; Format: ESC[#;#R   ;
  763. ;--------------------------------------------------------;
  764.  
  765. DEVICE_STATUS: MOV     DI,OFFSET DEVICE_STATUS_BUFFER
  766.                MOV     AL,ESC_CHAR
  767.                STOSB                           ;Store leading Esc, bracket
  768.                MOV     AL,"["                  ;in special Device Status Buffer.
  769.                STOSB
  770.  
  771.                MOV     AL,CURSOR_ROW           ;Retrieve cursor row.
  772.                XOR     AH,AH                   ;Zero in high half.
  773.                INC     AX                      ;Convert to base one.
  774.                MOV     BP,1                    ;Flag as storage.
  775.                CALL    DECIMAL_OUT             ;Convert to ASCII.
  776.                MOV     AL,";"                  ;Store delimiting semi-colon.
  777.                STOSB
  778.                MOV     AL,CURSOR_COL           ;Do same with cursor column.
  779.                XOR     AH,AH
  780.                INC     AX
  781.                CALL    DECIMAL_OUT
  782.                MOV     AL,"R"                  ;Add command character
  783.                STOSB
  784.                MOV     AL,CR                   ; and carriage return.
  785.                STOSB
  786.                SUB     DI,OFFSET DEVICE_STATUS_BUFFER   ;Setup for console out.
  787.                MOV     REASSIGN_COUNT,DI
  788.                MOV     REASSIGN_POS,OFFSET DEVICE_STATUS_BUFFER
  789.                MOV     REASSIGN_FLAG,ON
  790.                MOV     REMOVE_FLAG,OFF
  791.                RET
  792.  
  793. ;------------------------------------------------;
  794.  
  795. SAVE_CURSOR:   MOV     DX,CURSOR_POSN
  796.                MOV     SAVE_POSITION,DX
  797.                RET
  798.  
  799. ;------------------------------------------------;
  800.  
  801. RESTORE_CURS:  MOV     DX,SAVE_POSITION
  802.                JMP     SET_CURSOR
  803.  
  804. ;------------------------------------------------;
  805.  
  806. ERASE_IN_LINE: MOV     DX,CURSOR_POSN          ;Erase from the cursor to
  807.                MOV     CX,CRT_COLS             ; the end of the line, including
  808.                SUB     CL,DL                   ; the current cursor position.
  809.                CALL    CK_SLOW_TEXT
  810.                JC      ERASE_SLOW              ;If ANSI ON and not graphics,
  811.                CALL    VIDEO_SETUP             ; write directly to video buffer
  812.                MOV     AL,SPACE                ; with space/attribute.
  813.                MOV     AH,ATTRIBUTE
  814.                REP     STOSW
  815.                RET
  816.  
  817. ERASE_SLOW:    MOV     CX,DX                   ;Else, erase SLOW via
  818.                MOV     DL,BYTE PTR CRT_COLS    ; BIOS scroll active page.
  819.                DEC     DL
  820.                MOV     BH,ATTRIBUTE
  821.                MOV     AX,601H
  822.                INT     10H
  823.                RET
  824.  
  825. ;------------------------------------------------;
  826. ;            Set Graphics Rendition              ;
  827. ;------------------------------------------------;
  828.  
  829. SGR:           MOV     SI,OFFSET NUMBER_BUFFER     ;Point to number parameters.
  830. NEXT_SGR:      LODSB                               ;Retrieve parameter.
  831.                MOV     DI,OFFSET ATTRIBUTE_TABLE   ;Look up attribute in
  832.                MOV     CX,ATTRIBUTE_LENGTH         ; translation table.
  833.                REPNZ   SCASB
  834.                JNZ     SGR_LOOP
  835.  
  836.                MOV     DI,OFFSET ATTRIBUTE_END
  837.                SHL     CX,1
  838.                SUB     DI,CX
  839.                MOV     AX,[DI]
  840.                AND     ATTRIBUTE,AL            ;If match, AND with strip mask.
  841.                OR      ATTRIBUTE,AH            ;OR with add mask.
  842. SGR_LOOP:      DEC     NUMBER_COUNT            ;Do all parameters.
  843.                JNZ     NEXT_SGR
  844.                RET
  845.  
  846. ;------------------------------------------------;
  847.  
  848. SET_MODE:      MOV     AH,ON                   ;Assume command 7,
  849.                JMP     SHORT CK_WRAP           ; turn line wrap on.
  850.  
  851. ;------------------------------------------------;
  852.  
  853. RESET_MODE:    MOV     AH,OFF                     ;Assume turn line wrap off.
  854. CK_WRAP:       MOV     AL,BYTE PTR NUMBER_BUFFER  ;Retrieve number parameter.
  855.                CMP     AL,7                       ;Is it 7?
  856.                JZ      SET_WRAP                ;If yes, set wrap mode.
  857.                JB      DO_MODE                 ;1 - 6 valid modes.
  858.                CMP     AL,14                   ;14 - 16 valid modes.
  859.                JB      MODE_END
  860.                CMP     AL,19
  861.                JA      MODE_END                ;If above 16, illegal.
  862. DO_MODE:       XOR     AH,AH                   ;Else, set video mode
  863.                INT     10H                     ; to parameter.
  864.                RET
  865.  
  866. SET_WRAP:      MOV     LINE_WRAP,AH
  867. MODE_END:      RET
  868.  
  869. ;--------------------------------------------------------------------;
  870. ; Keyboard Key Reassignment:  First convert ASCII numbers to binary, ;
  871. ; parse out delimiting semi-colons, and quote string delimiters.     ;
  872. ;--------------------------------------------------------------------;
  873.  
  874. REASSIGNMENT:  MOV     CX,ESC_COUNT            ;Retrieve length of Esc sequence.
  875.                DEC     CX                          ;Adjust.
  876.                MOV     SI,OFFSET ESC_BUFFER + 2    ;Point past Esc, bracket.
  877.                MOV     DI,OFFSET PARSE_BUFFER      ;Point to parse storage.
  878. NEXT_STRING:   MOV     QUOTE_TYPE,0                ;Reset quote type.
  879. NEXT_NUM:      XOR     DL,DL                   ;Use DL to carry number; init = 0
  880.                XOR     BP,BP                   ;Use BP as number flag.
  881. NEXT_PARAM:    LODSB                           ;Retrieve a byte.
  882.                DEC     CX                      ;Decrement string length counter.
  883.                JZ      PARSE_END               ;If zero, done.
  884.                MOV     AH,QUOTE_TYPE           ;Else, retrieve quote type.
  885.                OR      AH,AH                   ;If zero, not in string.
  886.                JNZ     QUOTE_MODE              ;Else, go to string mode.
  887.                CALL    CK_QUOTE                ;Is character a quote?
  888.                JZ      NEXT_PARAM              ;If yes, ignore.
  889.                CMP     AL,";"                  ;Else, is it semi-colon?
  890.                JZ      STORE_NUMBER            ;If yes, number delimiter.
  891.  
  892.                SUB     AL,"0"                  ;Else, must be number; convert
  893.                MOV     DH,AL                   ; to binary; save in DH.
  894.                MOV     AX,10                   ;Multiply current total by ten.
  895.                MUL     DL
  896.                ADD     AL,DH                   ;Add new number.
  897.                MOV     DL,AL                   ;Save in DL.
  898.                MOV     BP,1                    ;Flag that number mode active.
  899.                JMP     SHORT NEXT_PARAM        ;Next parameter.
  900.  
  901. STORE_NUMBER:  OR      BP,BP                   ;If number mode flag not set then
  902.                JZ      NEXT_PARAM              ;semi-colon not prefaced with no.
  903.                MOV     AL,DL                   ;Else, store number.
  904.                STOSB
  905.                JMP     SHORT NEXT_NUM          ;Reset number carrying registers.
  906.  
  907. QUOTE_MODE:    CMP     AL,AH                   ;Is current char a string ending
  908.                JZ      NEXT_STRING             ; quote?  If yes, exit quote mode
  909.                STOSB                           ;Else, store char as literal.
  910.                JMP     SHORT NEXT_PARAM
  911.  
  912. ;----------------------------------------------------------------------------;
  913. ; Remove duplicate assignments if removal leaves room for new assignment.    ;
  914. ; If no duplicate and room, add new assignment, else flush buffer to screen. ;
  915. ;----------------------------------------------------------------------------;
  916.  
  917. PARSE_END:     OR      BP,BP                   ;Was last parameter a number?
  918.                JZ      CK_LENGTH               ;If no, skip.
  919.                MOV     AL,DL                   ;Else, store the last number.
  920.                STOSB
  921. CK_LENGTH:     SUB     DI,OFFSET PARSE_BUFFER - 2   ;Calculate string length
  922.                MOV     AX,DI                        ; including word for length.
  923.                MOV     BX,WORD PTR PARSE_BUFFER     ;BL=new first ASCII; BH=2nd.
  924.                MOV     CX,5                         ;String length has to be at
  925.                OR      BL,BL                   ;least word + 2 for old + new = 5
  926.                JZ      CK_LEGAL                ; for extended key reassignment.
  927.                DEC     CX                      ;And word + 1 for old + new = 4
  928. CK_LEGAL:      CMP     AX,CX                   ; for regular ASCII reassignment.
  929.                JB      ASSIGN_FLUSH            ;If not, illegal; flush buffer.
  930.                MOV     BP,BUFFER_END           ;BP to carry buffer end pointer.
  931.                CALL    CK_MATCH                ;Is char already reassigned?
  932.                JC      CK_ROOM                 ;If no, check room for new.
  933.  
  934. CK_REMOVE:     MOV     CX,DX                   ;REASSIGN_END - string end
  935.                SUB     CX,SI                   ; = bytes to move.
  936.                JZ      CK_ROOM                 ;If zero, last string.
  937.                SUB     DX,[DI]                 ;Is REASSIGN_END - old string
  938.                ADD     DX,AX                   ; + new string >
  939.                CMP     DX,BP                   ; BUFFER_END?
  940.                JA      ASSIGN_FLUSH            ;If yes, flush buffer to screen.
  941.                REP     MOVSB                   ;Else, move them down in buffer.
  942.                JMP     SHORT STORE_NEW
  943.  
  944. CK_ROOM:       ADD     DX,AX                   ;New string + current strings.
  945.                CMP     DX,BP                   ;Greater than buffer size?
  946.                JA      ASSIGN_FLUSH            ;If yes, flush new string.
  947. STORE_NEW:     MOV     SI,OFFSET PARSE_BUFFER  ;Else, room for new string.
  948.                STOSW                           ;Store string length first.
  949.                MOV     CX,AX                   ;Adjust counter.
  950.                DEC     CX
  951.                DEC     CX
  952.                REP     MOVSB                   ;Store the actual string.
  953.                MOV     REASSIGN_END,DI         ;Store new string end.
  954.                RET
  955.  
  956. ASSIGN_FLUSH:  MOV     AL,"p"                  ;If buffer full, flush string
  957.                JMP     FLUSH_BUFFER            ; including ending "p" command.
  958.  
  959. ;------------------------------------------------------------------------------;
  960. ; INPUT:  BL = first ASCII code to match; BH = extended if BL = 0              ;
  961. ; OUTPUT: CY = 1 if no match found; CY = 0 if match found.                     ;
  962. ;         DI points to string length of match; DI+2 points to start of string. ;
  963. ;         SI = end of string; DX = REASSIGN_END; BP = BUFFER_END               ;
  964. ;------------------------------------------------------------------------------;
  965.  
  966. CK_MATCH:      MOV     DX,REASSIGN_END                ;Current strings end.
  967.                MOV     SI,OFFSET REASSIGNMENT_BUFFER  ;Point to buffer.
  968. NEXT_MATCH:    MOV     DI,SI                          ;Current record.
  969.                CMP     DI,DX                   ;End of strings?
  970.                JAE     NO_MATCH                ;If yes, no match.
  971.                MOV     CX,[DI+2]               ;CL=current first ASCII; CH=2nd
  972.                ADD     SI,[DI]                 ;Point to next record.
  973.                CMP     BL,CL                   ;First characters match?
  974.                JNZ     NEXT_MATCH              ;If no, check next record.
  975.                OR      BL,BL                   ;Extended code? ie zero.
  976.                JNZ     MATCH                   ;If no, then match.
  977.                CMP     BH,CH                   ;Else, check second code.
  978.                JNZ     NEXT_MATCH              ;If not same, no match.
  979. MATCH:         CLC                             ;Else return with CY = 0
  980.                RET
  981.  
  982. NO_MATCH:      STC                             ;No match; CY = 1.
  983.                RET
  984.  
  985. ;----------------------------------------------------------------------;
  986. ; Buffer area will write over disposable data and initialization code. ;
  987. ;----------------------------------------------------------------------;
  988.  
  989. ESC_BUFFER            =       $
  990. NUMBER_BUFFER         =       ESC_BUFFER + ESC_BUFFER_SIZE
  991. PARSE_BUFFER          =       NUMBER_BUFFER
  992. DEVICE_STATUS_BUFFER  =       PARSE_BUFFER + ESC_BUFFER_SIZE
  993. DEVICE_STATUS_SIZE    =       11
  994. REASSIGNMENT_BUFFER   =       DEVICE_STATUS_BUFFER + DEVICE_STATUS_SIZE
  995.  
  996. ;              DISPOSABLE DATA
  997. ;              ---------------
  998.  
  999. SYNTAX         LABEL   BYTE
  1000. DB      "Syntax:  ANSI [FAST | SLOW][ON | OFF][/B nnn][/C][/U]",CR,LF
  1001. DB      "FAST = direct screen writes; default",CR,LF
  1002. DB      "SLOW = screen writes via BIOS",CR,LF
  1003. DB      "ON/OFF = active/inactive; default is ON",CR,LF
  1004. DB      "nnn = buffer size in bytes (0 - 60K) reserved for key reassignment; "
  1005. DB      "default 200",CR,LF
  1006. DB      "/U = Uninstall"
  1007. CR_LF   DB     CR,LF,LF,"$"
  1008.  
  1009. STATUS_MSG     DB      "Status: $"
  1010. BUFFER_MSG     DB      CR,LF,"Buffer size: $"
  1011. BYTES_FREE     DB      CR,LF,"Bytes free:  $"
  1012. ANSI_SYS_MSG   DB      "ANSI.SYS is installed so "
  1013. NOT_INSTALLED  DB      "ANSI.COM not installed",CR,LF,"$"
  1014. CON            DB      "CON"
  1015. CON_OFFSET     EQU     10
  1016.  
  1017. SIZE_MSG       DB      "Uninstall to change buffer size",CR,LF,LF,"$"
  1018. UNLOAD_MSG     DB      "ANSI can't be uninstalled",CR,LF
  1019.                DB      "Uninstall resident programs in reverse order",CR,LF,"$"
  1020. NOT_ENOUGH     DB      "Not enough memory",CR,LF,"$"
  1021. ALLOCATE_MSG   DB      "Memory allocation error",CR,LF,BELL,"$"
  1022. INSTALL_MSG    DB      "Installed",CR,LF,"$"
  1023. UNINSTALL_MSG  DB      "Uninstalled",CR,LF,"$"
  1024.  
  1025. ;--------------------------------------------------------------------;
  1026. ; Search memory for a copy of our code, to see if already installed. ;
  1027. ;--------------------------------------------------------------------;
  1028.  
  1029. INITIALIZE     PROC    NEAR
  1030.                CLD                             ;All string operations forward.
  1031.                MOV     BX,OFFSET START         ;Point to start of code.
  1032.                NOT     BYTE PTR [BX]           ;Change a byte so no false match.
  1033.                XOR     DX,DX                   ;Start at segment zero.
  1034.                MOV     AX,CS                   ;Store our segment in AX.
  1035. NEXT_PARAG:    INC     DX                      ;Next paragraph.
  1036.                MOV     ES,DX
  1037.                CMP     DX,AX                   ;Is it our segment?
  1038.                JZ      ANNOUNCE                ;If yes, search is done.
  1039.                MOV     SI,BX                   ;Else, point to our signature.
  1040.                MOV     DI,BX                   ; and offset of possible match.
  1041.                MOV     CX,16                   ;Check 16 bytes for match.
  1042.                REP     CMPSB
  1043.                JNZ     NEXT_PARAG              ;If no match, keep looking.
  1044.  
  1045. ;------------------------------------------------;
  1046.  
  1047. ANNOUNCE:      MOV     DX,OFFSET SIGNATURE     ;Display our signature.
  1048.                CALL    PRINT_STRING
  1049.                MOV     DX,OFFSET SYNTAX        ;And syntax.
  1050.                CALL    PRINT_STRING
  1051.                MOV     SI,81H                  ;Point to command line.
  1052. NEXT_CAP:      LODSB                           ;Capitalize parameters.
  1053.                CMP     AL,CR
  1054.                JZ      PARSE
  1055.                CMP     AL,"a"
  1056.                JB      NEXT_CAP
  1057.                CMP     AL,"z"
  1058.                JA      NEXT_CAP
  1059.                AND     BYTE PTR [SI - 1],5FH
  1060.                JMP     SHORT NEXT_CAP
  1061.  
  1062. ;------------------------------------------------;
  1063.  
  1064. PARSE:         MOV     SI,81H                  ;Point to command line again.
  1065. NEXT_PARA:     XOR     AX,AX                   ;Position in status parameters.
  1066.                MOV     BX,4                    ;Status parameters each 4 bytes.
  1067. NEXT_STATUS:   MOV     DI,OFFSET PARAMETERS    ;Point to "OFF ON  SLOWFAST"
  1068.                ADD     DI,AX                   ;Point to next parameter.
  1069.                ADD     AX,BX
  1070.                CMP     AX,LAST_PARAMETER       ;Check all four possible statuses
  1071.                JA      CK_SWITCHES
  1072.                PUSH    SI                      ;Save command line pointer.
  1073.                MOV     CX,2                    ;Check first two bytes for match.
  1074.                REP     CMPSB
  1075.                POP     SI                      ;Recover command line pointer.
  1076.                JNZ     NEXT_STATUS
  1077.  
  1078.                DIV     BL                      ;If match, divide offset by four.
  1079.                MOV     CL,1                    ;Set a bit to match offset.
  1080. NEXT_SHIFT:    SHL     CL,1
  1081.                DEC     AL
  1082.                JNZ     NEXT_SHIFT
  1083.                SHR     CL,1                    ;Adjust.
  1084.                MOV     AH,STATUS_MASK          ;Retrieve appropriate ON/OFF
  1085.                CMP     CL,ON                   ; or FAST/SLOW mask.
  1086.                JBE     ADD_STATUS
  1087.                ROL     AH,1
  1088.                ROL     AH,1
  1089.  
  1090. ADD_STATUS:    AND     ES:STATUS,AH            ;Mask off old status.
  1091.                OR      ES:STATUS,CL            ;Add new status.
  1092.                INC     SI                      ;Bump command line pointer.
  1093.                INC     SI
  1094.  
  1095. CK_SWITCHES:   LODSB                           ;Get a byte.
  1096.                CMP     AL,CR                   ;Is it carriage return?
  1097.                JZ      INSTALL                 ;If yes, done here.
  1098.                CMP     AL,"/"                  ;Is there a switch character?
  1099.                JNZ     NEXT_PARA               ;If no, keep looking.
  1100.                LODSB                           ;Else, get the switch character.
  1101.                CMP     AL,"B"                  ;Is it "B" ?
  1102.                JNZ     CK_C                    ;If no, check "C".
  1103.                CALL    CK_INSTALLED            ;Else, see if already installed.
  1104.                JZ      GET_SIZE                ;If no, get buffer request size.
  1105.                MOV     DX,OFFSET SIZE_MSG      ;Else, display error message.
  1106.                CALL    PRINT_STRING
  1107.                JMP     SHORT NEXT_PARA
  1108. GET_SIZE:      CALL    DECIMAL_INPUT           ;Get number parameter.
  1109.                CMP     BX,REASSIGNMENT_MAX     ;If greater than maximum, use
  1110.                JBE     STORE_BUFFER            ; maximum, else use requested
  1111.                MOV     BX,REASSIGNMENT_MAX     ; size.
  1112. STORE_BUFFER:  MOV     REASSIGNMENT_SIZE,BX
  1113.                ADD     BX,OFFSET REASSIGNMENT_BUFFER  ;Calculate end of buffer.
  1114.                MOV     BUFFER_END,BX
  1115.                JMP     SHORT NEXT_PARA
  1116.  
  1117. CK_C:          CMP     AL,"C"                  ;Is it "C" ?
  1118.                JNZ     CK_U                    ;If not, check "U".
  1119.                MOV     ES:REASSIGN_END,OFFSET REASSIGNMENT_BUFFER  ;Clear buffer
  1120. CK_U:          CMP     AL,"U"                  ;Is it "U" ?
  1121.                JZ      DO_U                    ;If yes, try to uninstall.
  1122.                JMP     NEXT_PARA               ;Else, next parmater.
  1123. DO_U:          CALL    CK_INSTALLED            ;Else, see if installed.
  1124.                MOV     DX,OFFSET NOT_INSTALLED ;If no, exit with error message.
  1125.                JZ      LILLY_PAD               ;Too far for short jump.
  1126.                JMP     UNINSTALL               ;Else, uninstall.
  1127.  
  1128. ;------------------------------------------------;
  1129.  
  1130. INSTALL:       CALL    STATUS_REPORT           ;Display status.
  1131.                CALL    CK_INSTALLED            ;Check if already installed.
  1132.                JZ      CK_AVAILABLE            ;If no, see if enough memory.
  1133.                OR      AL,AL                   ;Else, done.
  1134.                JMP     EXIT                    ;Exit with ERRORLEVEL = 0.
  1135.  
  1136. ;--------------------------------;
  1137. ; This is the install procedure. ;
  1138. ;--------------------------------;
  1139.  
  1140. CK_AVAILABLE:  MOV     BP,OFFSET REASSIGNMENT_BUFFER   ;TSR ends at end
  1141.                ADD     BP,REASSIGNMENT_SIZE            ; of reassignment buffer.
  1142.                ADD     BP,15                           ;Round up.
  1143.                CMP     BP,DS:[6]               ;Buffer > PSP bytes in segment?
  1144.                MOV     DX,OFFSET NOT_ENOUGH    ;If yes, exit without installing
  1145.                JA      MSG_EXIT                ; with message.
  1146.  
  1147.                MOV     AX,3529H                ;Get undocumented INT 29 vector.
  1148.                INT     21H
  1149.                MOV     SI,OFFSET CON           ;Does it point to ANSI.SYS?
  1150.                MOV     DI,CON_OFFSET           ;Check by looking for "CON"
  1151.                MOV     CX,3                    ; as device name.
  1152.                REP     CMPSB
  1153.                MOV     DX,OFFSET ANSI_SYS_MSG  ;Exit with error message if
  1154. LILLY_PAD:     JZ      MSG_EXIT                ;ANSI.SYS loaded.
  1155.  
  1156.                MOV     OLD_INT_29[0],BX        ;Else save old interrupt.
  1157.                MOV     OLD_INT_29[2],ES
  1158.                MOV     DX,OFFSET ANSI_INT_29   ;Install new interrupt.
  1159.                MOV     AX,2529H
  1160.                INT     21H
  1161.                MOV     AX,3516H                ;Get INT 16 vector.
  1162.                INT     21H
  1163.                MOV     OLD_INT_16[0],BX        ;Save old interrupt.
  1164.                MOV     OLD_INT_16[2],ES
  1165.                MOV     DX,OFFSET ANSI_INT_16   ;Install new interrupt.
  1166.                MOV     AX,2516H
  1167.                INT     21H
  1168.                MOV     AX,3521H                ;Get DOS 21h interrupt.
  1169.                INT     21H
  1170.                MOV     OLD_INT_21[0],BX        ;Save old interrupt.
  1171.                MOV     OLD_INT_21[2],ES
  1172.                MOV     DX,OFFSET ANSI_INT_21   ;Install new interrupt.
  1173.                MOV     AX,2521H
  1174.                INT     21H
  1175.  
  1176.                MOV     AX,DS:[2CH]             ;Get environment segment.
  1177.                MOV     ES,AX
  1178.                MOV     AH,49H                  ;Free up environment.
  1179.                INT     21H
  1180.  
  1181.                MOV     DX,OFFSET INSTALL_MSG   ;Display install message.
  1182.                CALL    PRINT_STRING
  1183.                CALL    FLUSH_END               ;Setup the number buffer.
  1184.                MOV     DX,BP                   ;Retrieve resident byte request.
  1185.                MOV     CL,4
  1186.                SHR     DX,CL                   ;Convert to paragraphs.
  1187.                MOV     AX,3100H                ;Return error code of zero.
  1188.                INT     21H                     ;Terminate but stay resident.
  1189.  
  1190. ;-------------------------------------------------------------------;
  1191. ; Exit.  Return ERRORLEVEL code 0 if successful, 1 if unsuccessful. ;
  1192. ;-------------------------------------------------------------------;
  1193.  
  1194. MSG_EXIT:      CALL    PRINT_STRING
  1195. ERROR_EXIT:    MOV     AL,1                    ;ERRORLEVEL = 1.
  1196. EXIT:          MOV     AH,4CH                  ;Terminate.
  1197.                INT     21H
  1198.  
  1199. ;---------------------------------------------------;
  1200. ; This subroutine uninstalls the resident ANSI.COM. ;
  1201. ;---------------------------------------------------;
  1202.  
  1203. UNINSTALL:     AND     ES:STATUS,NOT ON        ;Turn OFF ANSI, just in case
  1204.                OR      ES:STATUS,OFF           ; can't uninstall.
  1205.                MOV     CX,ES                   ;Save segment in CX.
  1206.                MOV     AX,3529H                ;Get interrupt 29h.
  1207.                INT     21H
  1208.                CMP     BX,OFFSET ANSI_INT_29   ;Has it been hooked by another?
  1209.                JNZ     UNINSTALL_ERR           ;If yes, exit with error message.
  1210.                MOV     BX,ES
  1211.                CMP     BX,CX                   ;Is the segment vector same?
  1212.                JNZ     UNINSTALL_ERR           ;If no, exit with error message.
  1213.  
  1214.                MOV     AX,3516H                ;Get interrupt 16h.
  1215.                INT     21H
  1216.                CMP     BX,OFFSET ANSI_INT_16   ;Has it been hooked by another?
  1217.                JNZ     UNINSTALL_ERR           ;If yes, exit with error message.
  1218.                MOV     BX,ES
  1219.                CMP     BX,CX                   ;Is the segment vector same?
  1220.                JNZ     UNINSTALL_ERR           ;If no, exit with error message.
  1221.  
  1222.                MOV     AX,3521H                ;Get interrupt 21h.
  1223.                INT     21H
  1224.                CMP     BX,OFFSET ANSI_INT_21   ;Has it been hooked by another?
  1225.                JNZ     UNINSTALL_ERR           ;If yes, exit with error message.
  1226.                MOV     BX,ES
  1227.                CMP     BX,CX                   ;Is the segment vector same?
  1228.                JNZ     UNINSTALL_ERR           ;If no, exit with error message.
  1229.  
  1230.                MOV     AH,49H                  ;Return memory to system pool.
  1231.                INT     21H
  1232.                MOV     DX,OFFSET ALLOCATE_MSG
  1233.                JC      MSG_EXIT                ;Display message if problem.
  1234.  
  1235.                MOV     DX,ES:OLD_INT_29[0]     ;Restore old INT 29.
  1236.                MOV     DS,ES:OLD_INT_29[2]
  1237.                MOV     AX,2529H
  1238.                INT     21H
  1239.                MOV     DX,ES:OLD_INT_16[0]     ;Restore old INT 16.
  1240.                MOV     DS,ES:OLD_INT_16[2]
  1241.                MOV     AX,2516H
  1242.                INT     21H
  1243.                MOV     DX,ES:OLD_INT_21[0]     ;Restore old INT 21.
  1244.                MOV     DS,ES:OLD_INT_21[2]
  1245.                MOV     AX,2521H
  1246.                INT     21H
  1247.  
  1248.                PUSH    CS
  1249.                POP     DS                      ;Point to our data.
  1250.                MOV     DX,OFFSET UNINSTALL_MSG ;Display uninstall message.
  1251.                CALL    PRINT_STRING
  1252.                OR      AL,AL                   ;Exit with ERRORLEVEL = 0.
  1253.                JMP     EXIT
  1254.  
  1255. UNINSTALL_ERR: MOV     ES,CX                   ;If error, display OFF status.
  1256.                CALL    STATUS_REPORT
  1257.                MOV     DX,OFFSET UNLOAD_MSG    ;And exit with error message.
  1258.                JMP     MSG_EXIT
  1259. INITIALIZE     ENDP
  1260.  
  1261. ;--------------------------------------------------;
  1262. ; INPUT:  SI points to parameter start.            ;
  1263. ; OUTPUT: SI points to parameter end; BX = number. ;
  1264. ;--------------------------------------------------;
  1265.  
  1266. DECIMAL_INPUT  PROC    NEAR
  1267.                XOR     BX,BX                   ;Start with zero as number.
  1268. NEXT_DECIMAL:  LODSB                           ;Get a character.
  1269.                CMP     AL,CR                   ;Carriage return?
  1270.                JZ      ADJUST_DEC              ;If yes, done here.
  1271.                CMP     AL,"/"                  ;Forward slash?
  1272.                JZ      ADJUST_DEC              ;If yes, done here.
  1273.                SUB     AL,"0"                  ;ASCII to binary.
  1274.                JC      NEXT_DECIMAL            ;If not between 0 and 9, skip.
  1275.                CMP     AL,9
  1276.                JA      NEXT_DECIMAL
  1277.                CBW                             ;Convert byte to word.
  1278.                XCHG    AX,BX                   ;Swap old and new number.
  1279.                MOV     CX,10                   ;Shift to left by multiplying
  1280.                MUL     CX                      ; last entry by ten.
  1281.                JC      DECIMAL_ERROR           ;If carry, too big.
  1282.                ADD     BX,AX                   ;Add new number and store in BX.
  1283.                JNC     NEXT_DECIMAL            ;If not carry, next number.
  1284. DECIMAL_ERROR: MOV     BX,-1                   ;Else, too big; return -1.
  1285.  
  1286. ADJUST_DEC:    DEC     SI                      ;Adjust pointer.
  1287.                RET
  1288. DECIMAL_INPUT  ENDP
  1289.  
  1290. ;-------------------------------------------------------;
  1291. ; OUTPUT: ZR = 1 if not installed; ZR = 0 if installed. ;
  1292. ;-------------------------------------------------------;
  1293.  
  1294. CK_INSTALLED:  MOV     AX,ES
  1295.                MOV     BX,CS
  1296.                CMP     AX,BX                   ;Compare segments.
  1297.                RET
  1298.  
  1299. ;------------------------------------------------;
  1300.  
  1301. STATUS_REPORT: MOV     DX,OFFSET STATUS_MSG
  1302.                CALL    PRINT_STRING            ;Display "Status: ".
  1303.                MOV     BL,ES:STATUS
  1304.                MOV     BH,1
  1305. NEXT_REPORT:   TEST    BL,BH
  1306.                JZ      LOOP_STATUS
  1307.  
  1308.                MOV     DL,BH
  1309.                MOV     AX,-1
  1310. NEXT_BIT:      INC     AX
  1311.                SHR     DL,1
  1312.                JNC     NEXT_BIT
  1313.                SHL     AX,1
  1314.                SHL     AX,1
  1315.                MOV     SI,OFFSET PARAMETERS    ;Display appropriate ON or OFF,
  1316.                ADD     SI,AX                   ; SLOW or FAST.
  1317.                MOV     CX,4
  1318. REPORT:        LODSB
  1319.                CALL    PRINT_CHAR
  1320.                LOOP    REPORT
  1321.  
  1322. LOOP_STATUS:   SHL     BH,1
  1323.                CMP     BH,FAST
  1324.                JBE     NEXT_REPORT
  1325.                MOV     DX,OFFSET BUFFER_MSG    ;Display "Buffer size: ".
  1326.                CALL    PRINT_STRING
  1327.                MOV     AX,ES:REASSIGNMENT_SIZE
  1328.                PUSH    AX
  1329.                XOR     BP,BP
  1330.                CALL    DECIMAL_OUT             ;Display the size.
  1331.                MOV     DX,OFFSET BYTES_FREE    ;Display "Bytes free: ".
  1332.                CALL    PRINT_STRING
  1333.                POP     AX
  1334.                ADD     AX,OFFSET REASSIGNMENT_BUFFER
  1335.                SUB     AX,ES:REASSIGN_END
  1336.                CALL    DECIMAL_OUT             ;Display the bytes free.
  1337.                MOV     DX,OFFSET CR_LF
  1338.                CALL    PRINT_STRING
  1339.                RET
  1340.  
  1341. ;------------------------------------------------;
  1342.  
  1343. PRINT_CHAR:    MOV     DL,AL
  1344.                MOV     AH,2                    ;Print character via DOS.
  1345.                JMP     SHORT DOS_INT
  1346.  
  1347. PRINT_STRING:  MOV     AH,9                    ;Print string via DOS.
  1348. DOS_INT:       INT     21H
  1349.                RET
  1350.  
  1351. _TEXT          ENDS
  1352.                END     START
  1353.